package com.pks.datacenter.service.basic;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pks.datacenter.config.RedisKeyConfig;
import com.pks.datacenter.dto.DbTableColumnInfo;
import com.pks.datacenter.entity.basic.DbField;
import com.pks.datacenter.entity.basic.DbInfo;
import com.pks.datacenter.entity.basic.DbSource;
import com.pks.datacenter.handler.RedisHandler;
import com.pks.datacenter.mapper.basic.DbFieldMapper;
import com.pks.datacenter.mapper.basic.DbInfoMapper;
import com.pks.datacenter.mapper.basic.DbSourceMapper;
import com.pks.datacenter.utils.SUtilAes;
import com.pks.datacenter.utils.SUtilDb;
import com.pks.util.SUtilQbuilder;
import com.pks.web.dto.ServiceResult;
import com.pks.web.mybatis.service.BasicServiceImpl;

@Component("dbFieldService")
public class DbFieldServiceImpl extends BasicServiceImpl<DbField, Long, DbFieldMapper> implements
		DbFieldService {

	@Autowired
	private DbFieldMapper dbFieldMapper;
	@Autowired
	private DbInfoMapper dbInfoMapper;
	@Autowired
	private RedisHandler redisHandler;
	@Autowired
	private DbSourceMapper dbSourceMapper;

	@Override
	protected DbFieldMapper getMapper() {
		return dbFieldMapper;
	}

	@Override
	public void inserts(List<DbField> list) {
		dbFieldMapper.inserts(list);
	}

	@Override
	public ServiceResult insertForGenByDb(Long dbId) {
		DbInfo dbInfo = dbInfoMapper.findById(dbId);
		if (dbInfo == null) {
			return ServiceResult.initError("数据不存在");
		}
		if (DbInfo.db_type_mysql != dbInfo.getDb_type()) {
			return ServiceResult.initError("当前仅支持mysql数据库");
		}
		DbSource dbSource = dbSourceMapper.findById(dbInfo.getDbSourceId());
		String db_password = SUtilAes.Decrypt(dbSource.getDb_password());
		List<DbTableColumnInfo> columnInfos = SUtilDb.queryTableColumnList(dbSource.getDb_type(),
				dbSource.getDb_ip(), dbSource.getDb_port(), dbSource.getDb_name(),
				dbSource.getDb_account(), db_password, dbInfo.getTable_name());
		List<DbField> inserts = new ArrayList<DbField>();
		long orders = 0;
		for (DbTableColumnInfo column : columnInfos) {
			orders++;
			if (DbTableColumnInfo.MYSQL_STRING_FIELDS.contains(column.getData_type())) {
				inserts.add(new DbField(dbId, column.getColumn_name(), column.getColumn_comment(),
						DbField.type_string, column.getColumn_type(), orders));
			} else if (DbTableColumnInfo.MYSQL_NUMBER_FIELDS.contains(column.getData_type())) {
				inserts.add(new DbField(dbId, column.getColumn_name(), column.getColumn_comment(),
						DbField.type_number, column.getColumn_type(), orders));
			} else if (DbTableColumnInfo.MYSQL_DATE_FIELDS.contains(column.getData_type())) {
				inserts.add(new DbField(dbId, column.getColumn_name(), column.getColumn_comment(),
						DbField.type_date, column.getColumn_type(), orders));
			}
		}
		for (DbField dbField : inserts) {
			dbField.insertBefore();
		}
		if (inserts.size() > 0) {
			dbFieldMapper.deleteByDbId(dbId);
			dbFieldMapper.inserts(inserts);
		}
		return ServiceResult.initSuccess(null);
	}

	// @Override
	// public DbField findDbFieldById(Long id) {
	// DbField dbField = null;
	// Object obj = redisHandler.get(RedisKeyConfig.KEY_INFO_DBFIELD_PREFID +
	// id);
	// if (obj != null) {
	// dbField = (DbField) obj;
	// } else {
	// dbField = dbFieldMapper.findById(id);
	// redisHandler.set(RedisKeyConfig.KEY_INFO_DBFIELD_PREFID + id, dbField, 10
	// * 24 * 3600);
	// }
	// return dbField;
	// }

	@SuppressWarnings("unchecked")
	@Override
	public List<DbField> findByDbId(Long dbId) {
		List<DbField> list = null;
		Object obj = redisHandler.get(RedisKeyConfig.KEY_LIST_DBFIELD_PREFIX + dbId);
		if (obj != null) {
			list = (List<DbField>) obj;
		} else {
			list = dbFieldMapper.find(SUtilQbuilder.initQbuilder("dbId", dbId), null, null,
					"orders");
			redisHandler.set(RedisKeyConfig.KEY_LIST_DBFIELD_PREFIX + dbId, list,
					RedisKeyConfig.CACHE_TIME);
		}
		return list;
	}

}
